<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>管理后台-新增角色</title>
    <header th:replace="header::html"/>
    <link rel="stylesheet" th:href="@{/ztree/css/zTreeStyle/zTreeStyle.css}"/>
    <link rel="stylesheet" th:href="@{/css/ztree-style.css}"/>
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>名称
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" lay-verify="name" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span class="x-red">*</span>将会成为角色名
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="description" class="layui-form-label">
                        <span class="x-red">*</span>描述
                    </label>
                    <div class="layui-input-inline">
                        <input type="description" id="description" name="description" lay-verify="required"
                               class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        该角色的定位或权限范围
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">权限</label>
                    <div>
                        <ul id="menuTree" class="ztree"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="submit" class="layui-btn" lay-filter="add" lay-submit="" permission="sys:role:add">添加</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script type="text/javascript" th:src="@{/ztree/js/jquery.ztree.all.min.js}"></script>
    <script type="text/javascript" th:src="@{/js/menu.js}"></script>
    <script type="text/javascript">
        layui.use(['form', 'layer'], function () {
            var form = layui.form,
                layer = layui.layer;

            // 初始化菜单树
            $.fn.zTree.init($("#menuTree"), getSetting(), getMenuTree());

            // 权限校验
            checkPermissoin();

            // 自定义验证规则
            form.verify({
                name: function (value) {
                    if (value.length < 4) {
                        return '角色名至少4个字符';
                    }
                }
            });

            // 监听提交
            form.on('submit(add)', function (data) {
                var permissionIds = getCheckedMenuIds();
                data.field.permissionIds = permissionIds;

                // 发异步请求，添加角色
                $.ajax({
                    url: '/role/add',
                    type: 'POST',
                    traditional: true, // 使用传统的方式进行序列化，使后台方便接收数组参数
                    data: data.field,
                    dataType: 'json',
                    beforeSend: function() {
                        layer.load();
                    },
                    success: function (res) {
                        if (res.httpCode != 200 || res.retCode != 0) {
                            layer.alert(res.retMsg);
                        } else {
                            layer.alert("添加成功", {icon: 6}, function () {
                                // 关闭当前frame
                                xadmin.close();
                                // 对父窗口进行刷新
                                xadmin.father_reload();
                            });
                        }
                    }
                });
                layer.closeAll();
                return false;
            });
        });
    </script>
</body>
</html>
